package com.wjbgn.electronicnian.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.*;
import com.wjbgn.electronicnian.enums.CommonReturnEnum;
import com.wjbgn.electronicnian.enums.TwoChoiceOneEnum;
import com.wjbgn.electronicnian.mapper.NianWhereToGoMapper;
import com.wjbgn.electronicnian.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description:
 * Create Date: 2022-01-20T11:06:12.895428300
 *
 * @author weirx
 * @version 1.0
 */
@Service
public class NianWhereToGoServiceImpl extends ServiceImpl<NianWhereToGoMapper, NianWhereToGoDO> implements NianWhereToGoService {

    @Autowired
    private NianService nianService;

    @Autowired
    private NianDumplingsService dumplingsService;

    @Autowired
    private NianWordsService wordsService;

    @Autowired
    private NianUserService nianUserService;

    @Autowired
    private NianStealDumplingsLogService nianStealDumplingsLogService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result whereToGo(long nianId) {
        //获取当前有谁家
        List<NianDO> list = nianService.lambdaQuery().ne(NianDO::getId, nianId).list();
        if (list.size() == 0) {
            return Result.success(CommonReturnEnum.NO_OTHER_NIAN.getCode(), CommonReturnEnum.NO_OTHER_NIAN.getName(), null);
        } else {
            //随机幸运小伙伴
            int randomInt = list.size() == 1 ? 1 : RandomUtil.randomInt(1, list.size());
            NianDO nianDO = list.get(randomInt - 1);

            if (nianDO.getHungryNum() == 0) {
                return Result.success(CommonReturnEnum.TIGER_BYE.getCode(), "【号外】：" + nianDO.getNianName() + "饿的离家出走了，如果你们看见了帮忙告诉他", null);
            }

            NianWhereToGoDO nianWhereToGoDO = new NianWhereToGoDO();
            nianWhereToGoDO.setNianId(nianId);
            nianWhereToGoDO.setNianWhereAre(nianDO.getId());
            // 随机偷饺子还是学说话
            TwoChoiceOneEnum random = TwoChoiceOneEnum.getRandom();
            // 偷饺子
            if (random.getCode() == 1) {
                nianWhereToGoDO.setNianWhatDid(TwoChoiceOneEnum.DUMPLINGS.getCode());
                //查看小伙伴家是否有饺子
                List<NianDumplingsDO> dumplingsDOS = dumplingsService.list(new QueryWrapper<>(new NianDumplingsDO(nianDO.getId())));
                if (dumplingsDOS.size() > 0) {
                    // 有饺子，偷一个
                    int randomInt1 = dumplingsDOS.size() == 1 ? 1 : RandomUtil.randomInt(1, dumplingsDOS.size());
                    NianDumplingsDO nianDumplingsDO = dumplingsDOS.get(randomInt1 - 1);
                    if (nianDumplingsDO.getDumplingsNum() == 1) {
                        //如果小伙伴只有一个这个饺子，那就删掉它
                        dumplingsService.removeById(nianDumplingsDO.getId());
                    } else {
                        // 小伙伴的饺子大于一个，那就剪掉一个
                        dumplingsService.updateById(new NianDumplingsDO(nianDumplingsDO.getId(), nianDumplingsDO.getDumplingsNum() - 1));
                    }
                    // 自己添加一个饺子
                    // 看看当前获得的饺子是否存在，存在就增加数量，否则就新增
                    nianWhereToGoDO.setNianDumplings(nianDumplingsDO.getDumplingsName());
                    this.save(nianWhereToGoDO);
                    List<NianDumplingsDO> nianDumplingsDOS = dumplingsService.list(new QueryWrapper<>(new NianDumplingsDO(nianId, nianDumplingsDO.getDumplingsName())));
                    if (nianDumplingsDOS.size() > 0) {
                        dumplingsService.updateById(new NianDumplingsDO(nianDumplingsDOS.get(0).getId(), nianDumplingsDOS.get(0).getDumplingsNum() + 1));
                    } else {
                        dumplingsService.save(new NianDumplingsDO(nianId, nianDumplingsDO.getDumplingsName(), nianDumplingsDO.getHungryNum(), 1));
                    }

                    // 记录偷饺子日志
                    // 获取自己的名字
                    NianDO self = nianService.getById(nianId);
                    nianStealDumplingsLogService.save(new NianStealDumplingsLogDO(nianDumplingsDO.getNianId(), self.getNianName(), nianDumplingsDO.getDumplingsName()));
                    return Result.success(CommonReturnEnum.DUMPLINGS.getCode(), "偷了" + nianDO.getNianName() + "一个【" + nianDumplingsDO.getDumplingsName() + "】,香~~", null);


                } else {
                    return Result.success(CommonReturnEnum.NO_DUMPLINGS.getCode(), nianDO.getNianName() + "连饺子都没有，真可怜啊~~", null);
                }
            } else {
                nianWhereToGoDO.setNianWhatDid(TwoChoiceOneEnum.WORDS1.getCode());
                // 学说话
                List<NianWordsDO> nianWordsDOS = wordsService.list(new QueryWrapper<>(new NianWordsDO(nianDO.getId())));
                if (nianWordsDOS.size() == 0) {
                    return Result.success(CommonReturnEnum.NO_WORDS.getCode(), nianDO.getNianName() + "还没学会说话呢~~笨死算了", null);
                } else {
                    NianWordsDO nianWordsDO = nianWordsDOS.get(nianWordsDOS.size() == 1 ? 0 : RandomUtil.randomInt(1, nianWordsDOS.size()) - 1);
                    // 获取这句话的年兽的主人名称
                    NianUserDO nianUserDO = nianUserService.getById(nianDO.getUserId());
                    //自己学会这句话
                    nianWhereToGoDO.setNianWords(nianWordsDO.getNianWords());
                    this.save(nianWhereToGoDO);
                    wordsService.save(new NianWordsDO(nianId, nianWordsDO.getNianWords(), nianUserDO.getUsername()));
                    // 返回
                    return Result.success(CommonReturnEnum.TAKE_A_WORDS.getCode(), "【" + nianUserDO.getUsername() + "】托我给您带句话儿：" + nianWordsDO.getNianWords(), null);
                }
            }
        }
    }
}